import Vue from 'vue'
import VueRouter from 'vue-router'  

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: 'login'},
  { path: '/login', component: () => import('../components/MyLogin.vue')},
  {
    path: '/home',
    component: () => import('../components/MyHome.vue'),
    children: [
      { path: 'users', component: () => import('../components/menus/MyUsers.vue')},
      { path: 'rights', component: () => import('../components/menus/MyRights.vue')},
      { path: 'orders', component: () => import('../components/menus/MyOrders.vue')},
      { path: 'goods', component: () => import('../components/menus/MyGoods.vue')},
      { path: 'settings', component: () => import('../components/menus/MySettings.vue')},
      { path: 'detail/:id', component:() => import('../components/user/MyUserDetail.vue'), props: true}
    ]
  },
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  // 禁止在导航栏中输入地址，跳转至后台页面
  if (to.path !== '/login') {
     const token = localStorage.getItem('token')
     if (token) return next()
     else return next('/login')
  } else {
     localStorage.removeItem('token')
     next()
  }
})

export default router